<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');

class Support_mod extends CI_Model {

    private $_dbTableSupport = 'support';
    private $_dbTableUsers = 'users';
    private $_dbTableImages = 'images';
    
    function __construct() {
        // Call the Model constructor
        parent::__construct();
        $this->load->library('session');
    }
    
    public function getUsers(){
        $adminId=$this->session->userdata('admin_id');
        $query = $this->db->select
                ("from as id_from, max(time) as last_message, "
                        . " (SELECT username FROM {$this->_dbTableUsers} WHERE {$this->_dbTableUsers}.`id` = {$this->_dbTableSupport}.`from`) as username_from, "
                        . "( SELECT name  FROM {$this->_dbTableImages} WHERE `private`=0 and {$this->_dbTableImages}.`user_id`= {$this->_dbTableSupport}.`from` ORDER BY main desc LIMIT 1) as image_name_from "
                )
                ->from($this->_dbTableSupport)
                ->from($this->_dbTableUsers)    
                ->group_by($this->_dbTableSupport.'.from')
                ->order_by('time','desc')
                ->where($this->_dbTableSupport . ".to = " . $adminId)                
                ->get();
                $errors = $this->db->_error_message();
        if (!empty($errors)) {
            throw new Exception("SQL Error: " . $errors);
        } else {
            
        }

        if ($query->num_rows() > 0) {
            $users = $query->result();
            $query->free_result();
        } else {
            $users = NULL;
        }
        $result = array();
        $row = new stdClass();
        foreach ($users as $user) {
                $row = $user;
                $query = $this->db->select(" COUNT(*) as num_messages ")
                        ->from($this->_dbTableSupport)
                        ->where('from = ' . $user->id_from)
                        ->where($this->_dbTableSupport . ".to = " . $adminId)
                        
                        ->get();
                //echo $this->db->last_query();
                $errors = $this->db->_error_message();
                if (!empty($errors)) {
                    throw new Exception("SQL Error: " . $errors);
                } else {
                    
                }

                if ($query->num_rows() > 0) {
                    $count = $query->row();
                    $row->num_messages = $count->num_messages;
                    $query->free_result();
                } else {
                   
                    $row->num_messages = 0;
                }
             $result[] = $row;
        }
        return $result;
    }
    
    public function getDialog($userId){
        $where = "";
        $to=$this->session->userdata('admin_id');
        if( isset($userId) && $userId != "" )
        {
          $where .= "( ";
          $where .=  $this->_dbTableSupport . ".from = " . $userId;
          $where .= " OR " . $this->_dbTableSupport . ".to = " . $userId;
          $where .= " )";
        
        if( isset($to) && $to != "" )
        {
          $where .= " AND ( " . $this->_dbTableSupport . ".to = " . $to; 
          $where .= " OR ";        
          $where .=  $this->_dbTableSupport . ".from = " . $to; 
          $where .= " ) ";
        }
        
        $this->db->select
                ("time, content, from, to, "
                        . " (SELECT username FROM {$this->_dbTableUsers} WHERE {$this->_dbTableUsers}.`id` = `from`) as username_from, "
                       
                        . " (SELECT username FROM {$this->_dbTableUsers} WHERE {$this->_dbTableUsers}.`id` = `to` ) as username_to, "
                        . "( SELECT name  FROM {$this->_dbTableImages} WHERE `private`=0 and {$this->_dbTableImages}.`user_id`= {$this->_dbTableSupport}.`from` ORDER BY main desc LIMIT 1) as image_name_from, "
                        . "( SELECT name  FROM {$this->_dbTableImages} WHERE `private`=0 and {$this->_dbTableImages}.`user_id`= {$this->_dbTableSupport}.`to` ORDER BY main desc LIMIT 1) as image_name_to "
                      
                );
                $this->db->from($this->_dbTableSupport);
                
                if($where != "")
                  $this->db->where($where);
                  $this->db->order_by('id', 'asc');
                  $query = $this->db->get();
               
        //echo $this->db->last_query();
        $errors = $this->db->_error_message();
        if (!empty($errors)) {
          throw new Exception("SQL Error: " . $errors);
        } else { }

        if ($query->num_rows() > 0) {
          $messages = $query->result();
          $query->free_result();
        } else {
          $messages = NULL;
        }
       }
       else {
          $messages = NULL;}
        return $messages;
    
    }
    public function insertMessage($userId, $message){
        $from=$this->session->userdata('admin_id');
        $data = array(
            'from'=>$from,
            'to'=>$userId,
            'content'=>$message,
            'seen'=>'0'
        );
        $this->db->insert($this->_dbTableSupport, $data);
    }
}
?>
